
import {createStore,applyMiddleware} from "redux"
import thunk from "redux-thunk";

// 状态
const state ={
    count:12,
    books:[
        {
            id:"01001",
            name:"青春无悔"
        }
    ]
}

// 对状态的处理（函数）
// 参数：
//    1、state：是旧的状态
//    2、action：
//         type：action的类型
//         payload：载荷，就是参数。
// 返回值：新的状态

const reducer = (state,action)=>{
    const {type,payload} = action;
    switch(type){
        case "ADDCOUNT":
                let obj =  {
                    ...state,
                    count:state.count+payload
                }
                console.log("obj",obj);   
                return obj;
        case "SUBCOUNT":return {
            ...state,
            count:state.count-payload
        }
        case "ADDBOOK":{
            state.books.push(payload);    
            return state
        }
        default:return state;
    }
}

export default createStore(reducer,state,applyMiddleware(thunk));